package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional  //开启事务
    @Override
    public void save(DishDTO dishDTO) {
        //新建dish对象，拷贝数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //插入菜品数据并返回id
        dishMapper.save(dish);

        //获取菜品列表，将其绑定菜品id
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
            }
            //调用口味列表插入数据
            dishFlavorMapper.saveBatch(dishFlavors);
        }

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //设置起始页码和条数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        //查询数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishPageQueryDTO,dishVO);

        Page<DishVO> dishPage = dishMapper.page(dishVO);

        //返回数据
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    /**
     * 根据id删除菜品
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteById(List<Long> ids) {
        //查询菜品是否在售
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //查询菜品是否绑定套餐
        List<Long> list = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (list != null && list.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }

    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //查询菜品
        Dish dish = dishMapper.getById(id);
        //查询对应的口味列表
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getFlavorByDishId(id);
        //封装返回数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        //复制数据，修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品数据
        dishMapper.update(dish);
        //修改口味列表，先删除再插入
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if (flavorList != null && flavorList.size() > 0) {
            //删除
            dishFlavorMapper.deleteByDishId(dish.getId());
            for (DishFlavor flavor : flavorList) {

                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.saveBatch(flavorList);
        }

    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> listByCategoryId(Long categoryId) {

        //定义返回数据列表
        List<DishVO> list = new ArrayList<>();

        //查询菜品列表
        List<Dish> dishList = dishMapper.list(categoryId);
        //如果菜品存在，则进行数据封装
        if (dishList != null && dishList.size() > 0) {
            //循环遍历菜品再根据菜品id去获取口味
            for (Dish dish : dishList) {
                List<DishFlavor> dishFlavorList = dishFlavorMapper.getFlavorByDishId(dish.getId());
                //如果口味列表不为空，再进行数据封装
                if(dishFlavorList != null && dishFlavorList.size() > 0) {
                    //复制菜品数据
                    DishVO dishVO = new DishVO();
                    BeanUtils.copyProperties(dish,dishVO);
                    //添加口味列表
                    dishVO.setFlavors(dishFlavorList);
                    //将口味列表放入List<DishVO>
                    list.add(dishVO);
                }
            }
        }
        //返回数据
        return list;
    }

    /**
     * 菜品起售或禁用
     *
     * @param status
     * @param id
     */
    @Override
    public void startOpStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }
}
